import { join } from "node:path";
import { createWindow } from "@lib/electron-app/factories/windows/create";
import { container } from "@main/shared/bindings";
import { TYPES } from "@main/shared/types";
import { WINDOW_SIZE } from "@shared/constants";
import { BrowserWindow, nativeImage, nativeTheme } from "electron";
import windowStateKeeper from "electron-window-state";
import icon from "../../resources/build/icons/302ai.png?asset";
import iconWin from "../../resources/build/icons/win-logo.ico?asset";
import { titleBarOverlayDark, titleBarOverlayLight } from "../config";
import { isDev, isLinux, isMac, isWin } from "../constant";
import type { ShortcutsService } from "../services/shortcuts-service";
import type { TabService } from "../services/tab-service";

export async function MainWindow() {
  const mainWindowState = windowStateKeeper({
    defaultWidth: WINDOW_SIZE.MIN_WIDTH,
    defaultHeight: WINDOW_SIZE.MIN_HEIGHT,
    fullScreen: false,
    maximize: false,
  });

  const iconFile = nativeImage.createFromPath(icon);
  const iconFileWin = nativeImage.createFromPath(iconWin);
  const { shouldUseDarkColors } = nativeTheme;

  const window = createWindow({
    id: "main",
    x: mainWindowState.x,
    y: mainWindowState.y,
    width: mainWindowState.width,
    height: mainWindowState.height,
    minWidth: WINDOW_SIZE.MIN_WIDTH,
    minHeight: WINDOW_SIZE.MIN_HEIGHT,
    autoHideMenuBar: true,
    transparent: isMac,
    frame: isLinux ? false : undefined,
    icon: isWin ? iconFileWin : iconFile,
    visualEffectState: "active",
    titleBarStyle: isMac ? "hiddenInset" : "hidden",
    titleBarOverlay: !isMac
      ? shouldUseDarkColors
        ? titleBarOverlayDark
        : titleBarOverlayLight
      : undefined,
    backgroundColor: shouldUseDarkColors ? "#121212" : "#FFFFFF",
    trafficLightPosition: isMac ? { x: 12, y: 12 } : undefined,
    ...(isLinux && {
      thickFrame: false,
      resizable: true,
      skipTaskbar: false,
    }),
    webPreferences: {
      preload: join(__dirname, "../preload/index.js"),
      sandbox: false,
      devTools: isDev,
      webgl: true,
    },
    roundedCorners: true,
  });

  mainWindowState.manage(window);

  window.webContents.on("did-finish-load", () => {
    window.show();
  });

  window.on("close", () => {
    BrowserWindow.getAllWindows().forEach((win) => {
      win.destroy();
    });
  });

  /*
   * * ******************************************************
   * * This is a special handling for iframe shortcut keys.
   * * ******************************************************
   */
  window.on("focus", async () => {
    const tabService = container.get<TabService>(TYPES.TabService);
    const isIframe = await tabService.isCurrentTabIframe();
    if (isIframe) {
      const shortcutsService = container.get<ShortcutsService>(
        TYPES.ShortcutsService,
      );
      shortcutsService.registerIframeGlobalShortcuts();
    }
  });
  window.on("blur", () => {
    const shortcutsService = container.get<ShortcutsService>(
      TYPES.ShortcutsService,
    );
    shortcutsService.unregisterIframeGlobalShortcuts();
  });
  // *******************************************************

  return window;
}
